#include "markdown_editer2.h"

markdown_editer2::markdown_editer2(QString FilePath, QWidget *parent):
    jsconn(nullptr)
    ,channel(nullptr)
    ,pre_area(nullptr)
    ,EditBaseWidget(FilePath, parent)
{

}

void markdown_editer2::init_extra_member_val()
{
    /*
    webChannel构造流程
    1. 实例化一个QWebEngineView,JsConntext(一个从QObject继承的对象，用来做中间转发)
    2. 实例化一个QWebEnginePage(this), 然后调用EngineView->setPage(Page)来设置当前页面
    3. 实例化QWebChannel(this),使用 View->page()->setWebChanel(channel)将channel和Page绑定
    4. 设置Page首页
    */
    pre_area = new QWebEngineView(this);
    jsconn = new JsContext(text_edit);
    MQwbEnginePage* page = new MQwbEnginePage(pre_area);
    pre_area->setPage(page);
    channel = new QWebChannel(dynamic_cast<QWidget*>(this) );
    channel->registerObject(QStringLiteral("content"), jsconn);
    page->setWebChannel(channel);
    page->setUrl(QUrl("qrc:/markdown2/index.html"));
}

void markdown_editer2::resize_editer(int w, int h)
{
    this->resize(w, h);
    if (text_edit){
        text_edit->resize(w,h);
    }
        int mid_point = (int) w/2;
        text_edit->resize(mid_point, h);
        pre_area->resize(mid_point, h);
        text_edit->move(0,0);
        pre_area->move(mid_point,0);
}



void markdown_editer2::InitUiStyle()
{
    text_edit->setPlainText(file_content);
    resize_editer(width(), height());
    
    file_edited();
    connect(text_edit, SIGNAL(textChanged()), this, SLOT(file_edited()));


}

void markdown_editer2::file_edited()
{
    file_new_content = text_edit->toPlainText();
    jsconn->setText(file_new_content);
}

